package com.mall.service.impl;

import com.mall.common.utils.FtpUtil;
import com.mall.common.utils.IDUtils;
import com.mall.service.PictureService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/*
*
* 图片上传服务
*
* */

@Service
public class PictureServiceImpl implements PictureService{

    @Value("${FTP_ADDRESS}")
    private String FTP_ADDRESS;

    @Value("${FTP_PORT}")
    private Integer FTP_PORT;

    @Value("${FTP_USERNAME}")
    private String FTP_USERNAME;

    @Value("${FTP_PWD}")
    private String FTP_PWD;

    @Value("${FTP_BASE_PATH}")
    private String FTP_BASE_PATH;

    @Value("${IMAGE_BASE_URL}")
    private String IMAGE_BASE_URL;

    @Override
    public Map uploadPicture(MultipartFile multipartFile){
        Map resultMap=new HashMap();

        try {
            String oldName=multipartFile.getOriginalFilename();

            String newName= IDUtils.genImageName();
            newName=newName+oldName.substring(oldName.lastIndexOf("."));

            String imagePath=new DateTime().toString("/yyyy/MM/DD");


            boolean result=FtpUtil.uploadFile(FTP_ADDRESS,FTP_PORT,FTP_USERNAME,FTP_PWD,FTP_BASE_PATH,
                    imagePath,newName,multipartFile.getInputStream());

            if(!result){
                resultMap.put("error",1);
                resultMap.put("message","文件上传失败");
                return resultMap;
            }

            resultMap.put("error",0);
            resultMap.put("url",IMAGE_BASE_URL+imagePath+"/"+newName);

            return resultMap;

        } catch (IOException e) {
            resultMap.put("error",1);
            resultMap.put("message","文件上传发生异常");
            return resultMap;
        }
    }
}
